/**
 * Created by zy on 2016/6/13.
 * 重点关注 - 管体缺陷仪表板 - 管体缺陷
 */

;(function ($, window, document, undefined) {

    /*** 管道联动操作 - start ***/
    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        mileType: 'float',
        param: {
            'pipeLineId': $pipeline,
            'pipeSegmentId': $segment
        }
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        mileType: 'float',
        param: {
            'pipeLineId': $pipeline,
            'pipeSegmentId': $segment,
            'beginSignId': $begin
        }
    });

    // 填充管段
    function fillSegment(id) {
        if (id) {
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            }, {async: false});
        } else {
            $segment.add([]);
        }
    }

    // 填充起点&终点
    function fillPos(id, tag) {
        if (id) {
            if (tag === 'pipe') {
                DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                    var begin = DyyBase.getPos(dataset, 'begin', true);
                    var end = DyyBase.getPos(dataset, 'end', true);
                    $begin.asset(begin);
                    $end.asset(end);
                }, {async: false});
            } else if (tag === 'segment') {
                DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                    var begin = DyyBase.getPos(dataset, 'begin', true);
                    var end = DyyBase.getPos(dataset, 'end', true);
                    $begin.asset(begin);
                    $end.asset(end);
                }, {async: false});
            }
        } else {
            $begin.add([]);
            $end.add([]);
        }
    }

    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        fillSegment(id);
        fillPos(id, 'pipe');
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        fillPos(id, 'segment');
    });

    // 默认触发第一条管道
    $pipeline.element.trigger('select');
    /*** 管道联动操作 - end ***/

    // 搜索条件
    var filter = {
        getCondition: function () {
            var data = {};
            data.mode = true;
            // 管道
            data.lineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                } else {
                    $pipeline.TIP({content: '管道名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 管段
            data.segmentId = function () {
                var v = $segment.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            // 起点位置
            data.beginMileage = function () {
                return $begin.val('mile');
            }();
            // 终点位置
            data.endMileage = function () {
                return $end.val('mile');
            }();
            return DyyBase.convertToArr(data);
        }
    };

    // 重点显示模板
    var focusTemplate = {
        // 金属损失
        jsss: function (data) {
            var div = '<div class="zy-focus-table">';
            div += '<div><dl class="zy-focus-dl w-80"><dt><i class="zy-focus-icon-jsss"></i></dt><dd class="f16 fwb">金属损失</dd><dd class="f12 c-78">重点关注缺陷类型</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-50"><dt><i class="zy-count-icon"></i></dt><dd class="f16 fwb">' + data.NUM + '</dd><dd class="f12 c-78">数量</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-size-icon"></i></dt><dd class="f16 fwb">' + data.percent + '</dd><dd class="f12 c-78">所占总缺陷比例</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-repair-icon"></i></dt><dd class="f16 fwb">' + data.LJWX + '</dd><dd class="f12 c-fb">立即维修缺陷</dd></dl></div></div>';
            return div;
        },
        // 椭圆变形
        tybx: function (data) {
            var div = '<div class="zy-focus-table">';
            div += '<div><dl class="zy-focus-dl w-80"><dt><i class="zy-focus-icon-tybx"></i></dt><dd class="f16 fwb">椭圆变形</dd><dd class="f12 c-78">重点关注缺陷类型</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-50"><dt><i class="zy-count-icon"></i></dt><dd class="f16 fwb">' + data.NUM + '</dd><dd class="f12 c-78">数量</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-size-icon"></i></dt><dd class="f16 fwb">' + data.percent + '</dd><dd class="f12 c-78">所占总缺陷比例</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-repair-icon"></i></dt><dd class="f16 fwb">' + data.LJWX + '</dd><dd class="f12 c-78">立即维修缺陷</dd></dl></div></div>';
            return div;
        },
        // 损伤
        ss: function (data) {
            var div = '<div class="zy-focus-table">';
            div += '<div><dl class="zy-focus-dl w-80"><dt><i class="zy-focus-icon-ss"></i></dt><dd class="f16 fwb">损伤</dd><dd class="f12 c-78">重点关注缺陷类型</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-50"><dt><i class="zy-count-icon"></i></dt><dd class="f16 fwb">' + data.NUM + '</dd><dd class="f12 c-78">数量</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-size-icon"></i></dt><dd class="f16 fwb">' + data.percent + '</dd><dd class="f12 c-78">所占总缺陷比例</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-repair-icon"></i></dt><dd class="f16 fwb">' + data.LJWX + '</dd><dd class="f12 c-78">立即维修缺陷</dd></dl></div></div>';
            return div;
        },
        // 类裂纹
        llw: function (data) {
            var div = '<div class="zy-focus-table">';
            div += '<div><dl class="zy-focus-dl w-80"><dt><i class="zy-focus-icon-llw"></i></dt><dd class="f16 fwb">类裂纹</dd><dd class="f12 c-78">重点关注缺陷类型</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-50"><dt><i class="zy-count-icon"></i></dt><dd class="f16 fwb">' + data.NUM + '</dd><dd class="f12 c-78">数量</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-size-icon"></i></dt><dd class="f16 fwb">' + data.percent + '</dd><dd class="f12 c-78">所占总缺陷比例</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-repair-icon"></i></dt><dd class="f16 fwb">' + data.LJWX + '</dd><dd class="f12 c-78">立即维修缺陷</dd></dl></div></div>';
            return div;
        },
        // 焊缝异常
        hfyc: function (data) {
            var div = '<div class="zy-focus-table">';
            div += '<div><dl class="zy-focus-dl w-80"><dt><i class="zy-focus-icon-hfyc"></i></dt><dd class="f16 fwb">焊缝异常</dd><dd class="f12 c-78">重点关注缺陷类型</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-50"><dt><i class="zy-count-icon"></i></dt><dd class="f16 fwb">' + data.NUM + '</dd><dd class="f12 c-78">数量</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-size-icon"></i></dt><dd class="f16 fwb">' + data.percent + '</dd><dd class="f12 c-78">所占总缺陷比例</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-repair-icon"></i></dt><dd class="f16 fwb">' + data.LJWX + '</dd><dd class="f12 c-78">立即维修缺陷</dd></dl></div></div>';
            return div;
        },
        // 凹陷
        ox: function (data) {
            var div = '<div class="zy-focus-table">';
            div += '<div><dl class="zy-focus-dl w-80"><dt><i class="zy-focus-icon-ox"></i></dt><dd class="f16 fwb">凹陷</dd><dd class="f12 c-78">重点关注缺陷类型</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-50"><dt><i class="zy-count-icon"></i></dt><dd class="f16 fwb">' + data.NUM + '</dd><dd class="f12 c-78">数量</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-size-icon"></i></dt><dd class="f16 fwb">' + data.percent + '</dd><dd class="f12 c-78">所占总缺陷比例</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-size-icon"></i></dt><dd class="f16 fwb">' + data.gt6 + '</dd><dd class="f12 c-78">凹陷深度≥6%OD的缺陷个数</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-repair-icon"></i></dt><dd class="f16 fwb">' + data.LJWX + '</dd><dd class="f12 c-78">立即维修缺陷</dd></dl></div></div>';
            return div;
        },
        // 为空
        empty: function () {
            var div = '<div class="zy-focus-table">';
            div += '<div><dl class="zy-focus-dl w-80"><dt><i class="zy-focus-icon-jsss"></i></dt><dd class="f16 fwb">-</dd><dd class="f12 c-78">重点关注缺陷类型</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-50"><dt><i class="zy-count-icon"></i></dt><dd class="f16 fwb">-</dd><dd class="f12 c-78">数量</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-size-icon"></i></dt><dd class="f16 fwb">-</dd><dd class="f12 c-78">所占总缺陷比例</dd></dl></div>';
            div += '<div><dl class="zy-focus-dl w-75"><dt><i class="zy-repair-icon"></i></dt><dd class="f16 fwb">-</dd><dd class="f12 c-78">立即维修缺陷</dd></dl></div></div>';
            return div;
        }
    };

    // 数据格式化
    var dataFormat = {
        // 获得缺陷变化趋势数据
        getChangeTrendTable: function (data) {
            var result = data.result;
            // 排除数组对象 将其他对象 NUM 值 push 入一个数组
            var arr = [], total = 0;
            for (var x in result) {
                if (!($.isArray(result[x]))) {
                    arr.push(result[x].NUM);
                }
            }
            // 总数
            for (var k = 0; k < arr.length; k++) {
                total += arr[k];
            }
            // 为0将不再执行
            if (total != 0) {
                // 冒泡
                for (var i = 0; i < arr.length - 1; i++) {
                    for (var j = 0; j < arr.length - i - 1; j++) {
                        if (arr[j] > arr[j + 1]) {
                            var swap = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = swap;
                        }
                    }
                }
                // 找到拥有最大值的对象
                for (var l in result) {
                    if (result[l].NUM == arr[(arr.length - 1)]) {
                        if (result[l].NUM > 0) {
                            result[l].percent = (Math.round((result[l].NUM / total) * 10000) / 100.00) + '%';
                        }
                        $('#JQ_focus_on_panel').empty().html(focusTemplate[l](result[l]));
                        break;
                    }
                }
            } else {
                $('#JQ_focus_on_panel').empty().html(focusTemplate.empty());
            }

            // 文本
            $('#JQ_erf_erf').text(result.hint[0].erf);
            $('#JQ_erf_num').text(result.hint[0].num);

            $('#JQ_depth_erf').text(result.hint[2].depth);
            $('#JQ_depth_level').text(result.hint[2].areaLevel);
            $('#JQ_depth_num').text(result.hint[2].num);

            $('#JQ_area_erf').text(result.hint[1].erf);
            $('#JQ_area_level').text(result.hint[1].areaLevel);
            $('#JQ_area_num').text(result.hint[1].num);

            $('#JQ_length_length').text(result.hint[3].length);
            $('#JQ_length_width').text(result.hint[3].width);
            $('#JQ_length_area_level').text(result.hint[3].areaLevel);
            $('#JQ_length_num').text(result.hint[3].num);

            // 表格
            var data_01 = '<p><span style="width: 16%">金属损失</span>'
                + '<span style="width: 12%">' + result['jsss'].ERF + '</span>'
                + '<span style="width: 12%">' + result['jsss'].LJWX + '</span>'
                + '<span style="width: 12%">' + result['jsss'].YWX + '</span>'
                + '<span style="width: 12%">' + result['jsss'].ZZWX + '</span>'
                + '<span style="width: 12%">' + result['jsss'].NYWX + '</span>'
                + '<span style="width: 12%">' + result['jsss'].WWX + '</span>'
                + '<span style="width: 12%">' + result['jsss'].DHG + '</span></p>';

            var data_02 = '<p><span style="width: 16%">凹陷</span>'
                + '<span style="width: 12%">' + result['ox'].ERF + '</span>'
                + '<span style="width: 12%">' + result['ox'].LJWX + '</span>'
                + '<span style="width: 12%">' + result['ox'].YWX + '</span>'
                + '<span style="width: 12%">' + result['ox'].ZZWX + '</span>'
                + '<span style="width: 12%">' + result['ox'].NYWX + '</span>'
                + '<span style="width: 12%">' + result['ox'].WWX + '</span>'
                + '<span style="width: 12%">' + result['ox'].DHG + '</span></p>';

            var data_03 = '<p><span style="width: 16%">焊缝异常</span>'
                + '<span style="width: 12%">' + result['hfyc'].ERF + '</span>'
                + '<span style="width: 12%">' + result['hfyc'].LJWX + '</span>'
                + '<span style="width: 12%">' + result['hfyc'].YWX + '</span>'
                + '<span style="width: 12%">' + result['hfyc'].ZZWX + '</span>'
                + '<span style="width: 12%">' + result['hfyc'].NYWX + '</span>'
                + '<span style="width: 12%">' + result['hfyc'].WWX + '</span>'
                + '<span style="width: 12%">' + result['hfyc'].DHG + '</span></p>';

            var data_04 = '<p><span style="width: 16%">椭圆变形</span>'
                + '<span style="width: 12%">' + result['tybx'].ERF + '</span>'
                + '<span style="width: 12%">' + result['tybx'].LJWX + '</span>'
                + '<span style="width: 12%">' + result['tybx'].YWX + '</span>'
                + '<span style="width: 12%">' + result['tybx'].ZZWX + '</span>'
                + '<span style="width: 12%">' + result['tybx'].NYWX + '</span>'
                + '<span style="width: 12%">' + result['tybx'].WWX + '</span>'
                + '<span style="width: 12%">' + result['tybx'].DHG + '</span></p>';

            var data_05 = '<p><span style="width: 16%">类裂纹</span>'
                + '<span style="width: 12%">' + result['llw'].ERF + '</span>'
                + '<span style="width: 12%">' + result['llw'].LJWX + '</span>'
                + '<span style="width: 12%">' + result['llw'].YWX + '</span>'
                + '<span style="width: 12%">' + result['llw'].ZZWX + '</span>'
                + '<span style="width: 12%">' + result['llw'].NYWX + '</span>'
                + '<span style="width: 12%">' + result['llw'].WWX + '</span>'
                + '<span style="width: 12%">' + result['llw'].DHG + '</span></p>';

            var data_06 = '<p><span style="width: 16%">损伤</span>'
                + '<span style="width: 12%">' + result['ss'].ERF + '</span>'
                + '<span style="width: 12%">' + result['ss'].LJWX + '</span>'
                + '<span style="width: 12%">' + result['ss'].YWX + '</span>'
                + '<span style="width: 12%">' + result['ss'].ZZWX + '</span>'
                + '<span style="width: 12%">' + result['ss'].NYWX + '</span>'
                + '<span style="width: 12%">' + result['ss'].WWX + '</span>'
                + '<span style="width: 12%">' + result['ss'].DHG + '</span></p>';

            return data_01 + data_02 + data_03 + data_04 + data_05 + data_06;
        }
    };

    // 搜索
    $('#JQ_search_btn_01').click(function () {
        var param = filter.getCondition();
        if (param[0].value) {
            DyyBase.ajax(defectApi.url_board_focus(), param, function (dataset) {
                var result = dataFormat.getChangeTrendTable(dataset);
                if (result.length > 0) {
                    $('#JQ_focus_on_table').empty().html(result);

                    // 查询成功为导出添加成功状态
                    $('#JQ_export_btn_01').attr('data-status', 'true');
                }
            });
        }
    });

})(jQuery, window, document);